/**
 * @brief 菜单文件
 * @note MainMenuLength 定义了选项的总个数
 * MaxDisplayLine 定义了一页能显示的个数
 * 此文件依赖于显示和按键
 */
#include "menu.h"
#include "display.h"
#include "ABS_key.h"
#define MainMenuLength 10
#define MaxDisplayLine 4
#if (MainMenuLength % MaxDisplayLine) == 0
#define MaxPages (MainMenuLength / MaxDisplayLine)
#else
#define MaxPages (MainMenuLength / MaxDisplayLine + 1)
#endif
/// @brief 测试函数
/// @return 
My_StatusTypeDef noDo()
{
    int key_Num;
    static int num = 4;
    //displayClean();
    displayNum(num, Display_firstCol, Display_firstRow);
    key_Num = key_scan(0);
    if (key_Num == Key_ESC) {
        return MY_OK;
    } else if (key_Num == Key_OK) {
        return MY_STANDBY;
    } else if (key_Num == Key_Down) {
        num--;
    } else if (key_Num == Key_UP) {
        num++;
    }

    return MY_BUSY;
}
menuList_t mainMenuList[MainMenuLength] = {
    {"1", noDo},
    {"2", noDo},
    {"3", noDo},
    {"4", noDo},
    {"5", noDo},
    {"6", noDo},
    {"6", noDo},
    {"6", noDo},
    {"6", noDo},
    {"6", noDo},
   
};
/// @brief 显示光标 @note 仅供menuDisplay() 调用
/// @param cursor 光标的行数
static void displayCursor(int cursor)
{
    static int temp = 1;
    if (cursor == temp) {
        displayStr("<", Display_lastCol, (cursor - 1) * Display_spaced, RED);
    } else {
        displayStr(" ", Display_lastCol, (temp - 1) * Display_spaced, RED);
        displayStr("<", Display_lastCol, (cursor - 1) * Display_spaced, RED);
        temp = cursor;
    }
}
/**
 *@brief 菜单显示
 *@note 按向上向下可以控制光标，光标的显示位置的x坐标是通过宏定义(Display_lastCol)来决定的
 *      按OK键进入执行函数,等待函数返回,返回
 *      @arg MY_BUSY再次执行此函数，
 *      @arg MY_STANDBY为暂停执行，等待外部确认，外部按OK就继续执行，按ESC就退出，
 *      @arg MY_OK为退出
 */
void menuDisplay()
{
    int i;
    static int keyNum        = 0; // 0表示没有按键按下
    static int cursor        = 1; // 光标的行数(>=1)
    static int pages         = 1; // 页数(>=1)
    My_StatusTypeDef runFlag = MY_STANDBY;
#if (MainMenuLength % MaxDisplayLine == 0)
    for (i = 0; i < MaxDisplayLine; i++) {
        displayStr(mainMenuList[i + (pages - 1) * MaxDisplayLine].str, Display_firstCol, i * Display_spaced, RED);
    }
#else
    if (pages == MaxPages) {
        for (i = 0; i < MainMenuLength % MaxDisplayLine; i++) {
            displayStr(mainMenuList[i + (pages - 1) * MaxDisplayLine].str, Display_firstCol, i * Display_spaced, RED);
        }
    } else {
        for (i = 0; i < MaxDisplayLine; i++) {
            displayStr(mainMenuList[i + (pages - 1) * MaxDisplayLine].str, Display_firstCol, i * Display_spaced, RED);
        }
    }
#endif
    displayCursor(cursor);
    keyNum = key_scan(0);
    switch (keyNum) {
        case Key_Down:
            cursor++;
            if (cursor > MaxDisplayLine) {
                pages++;
                cursor = 1;
                displayClean();
            }

#if (MainMenuLength % MaxDisplayLine == 0)
            if (pages > MainMenuLength / MaxDisplayLine) {
                pages  = 1;
                cursor = 1;
                displayClean();
            }
#else
            if (pages == MaxPages) {

                if (cursor > (MainMenuLength % MaxDisplayLine)) {
                    pages  = 1;
                    cursor = 1;
                }
            }
#endif

            break;
        case Key_UP:
            cursor--;
            if (cursor <= 0) {
                pages--;
                if (pages <= 0) {
                    pages = MaxPages;

#if (MainMenuLength % MaxDisplayLine == 0)
                    cursor = MaxDisplayLine;
#else
                    cursor = MainMenuLength % MaxDisplayLine;
#endif
                    displayClean();
                } else {
                    cursor = MaxDisplayLine;
                }
            }
            break;
        case Key_OK:
            displayClean();
            while (1) {
                runFlag = mainMenuList[(cursor - 1 + (pages - 1) * MaxDisplayLine)].exe();
                if (MY_STANDBY == runFlag) {
                    while (1) {
                        keyNum = key_scan(0);
                        if (keyNum == Key_ESC) {
                            runFlag = MY_OK;
                            break;
                        } else if (keyNum == Key_OK) {
                            break;
                        }
                    }
                }
                if (runFlag == MY_OK) {
                    break;
                }
            }
            displayClean();
            break;
        default:
            break;
    }
}
